# -*- coding: utf-8 -*-
'''
@文件    :system.py
@说明    :系统管理
@时间    :2024/11/05 18:24:57
@作者    :浪晋
@版本    :1.0
'''

from tortoise import fields
from .base import BaseModel, TimestampMixin, DefaultFilterManager


class SystemMenuModel(BaseModel, TimestampMixin):
    title = fields.CharField(max_length=20, null=False, description="显示名称")
    parent_id = fields.IntField(default=0, max_length=10, description="父菜单ID")
    type = fields.CharField(max_length=100, null=False, description="菜单类型")
    name = fields.CharField(max_length=100, null=False, description="别名")
    icon = fields.CharField(max_length=100, null=False, description="菜单图标")
    path = fields.CharField(max_length=100, null=False, description="菜单路径")
    redirect = fields.CharField(max_length=100, null=True, description="重定向")
    active = fields.CharField(max_length=100, null=True, description="菜单高亮")
    component = fields.CharField(max_length=100, null=False, description="组件")
    color = fields.CharField(max_length=100, null=True, description="颜色")
    hidden = fields.BooleanField(default=False, null=True, description="隐藏菜单")
    hiddenBreadcrumb = fields.BooleanField(default=False, null=True, description="隐藏面包屑")
    fullpage = fields.BooleanField(default=False, null=True, description="整页路由")
    tag = fields.CharField(max_length=100, null=True, description="标签")
    order = fields.IntField(default=0, description="排序")
    apiList = fields.JSONField(default=[], null=True, description="接口权限")

    class Meta:
        table = "tb_system_menu"
        table_description = "系统菜单表"
        manager = DefaultFilterManager()


class SystemDeptModel(BaseModel, TimestampMixin):
    name = fields.CharField(max_length=20, null=False, description="部门名称")
    path = fields.CharField(max_length=20, null=False, description="路径")
    desc = fields.CharField(max_length=500, null=True, description="备注")
    order = fields.IntField(default=0, null=False, description="排序")
    parent_id = fields.IntField(default=0, max_length=10, description="父部门ID")
    active = fields.BooleanField(default=False, description="是否激活")

    class Meta:
        table = "tb_system_dept"
        table_description = "系统部门表"
        manager = DefaultFilterManager()


class SystemRoleModel(BaseModel, TimestampMixin):
    name = fields.CharField(max_length=20, null=False, description="角色名称")
    alias = fields.CharField(max_length=20, null=True, description="别名")
    desc = fields.CharField(max_length=500, null=True, description="角色描述")
    active = fields.BooleanField(default=False, null=False, description="是否激活")
    menus = fields.ManyToManyField("models.SystemMenuModel", through="tb_system_role_menu", forward_key="menu_id", backward_key="role_id",  description="角色菜单表")
    class Meta:
        table = "tb_system_role"
        table_description = "系统角色表"
        manager = DefaultFilterManager()


class SystemUserModel(BaseModel, TimestampMixin):
    account = fields.CharField(max_length=20, null=False, description="账号")
    realname = fields.CharField(max_length=30, null=False, description="姓名")
    nickname = fields.CharField(max_length=30, null=False, description="昵称")
    avatar = fields.CharField(max_length=255, null=True, description="头像")
    email = fields.CharField(max_length=255, null=True, description="邮箱")
    phone = fields.CharField(max_length=20, null=False, description="电话")
    password = fields.CharField(max_length=128, null=False, description="密码")
    superuser = fields.BooleanField(default=False, description="是否超级管理员")
    permission = fields.SmallIntField(null=True, description="数据权限")
    active = fields.BooleanField(default=False, description="是否激活")
    last_login_type = fields.SmallIntField(null=True, description="最后登录类型")
    last_login_time = fields.DatetimeField(null=True, description="最后登录时间")
    last_login_ip = fields.CharField(max_length=128, null=True, description="最后登录Ip")
    dept = fields.ForeignKeyField("models.SystemDeptModel", related_name="users", null=True, db_constraint=False, description="部门")
    roles = fields.ManyToManyField("models.SystemRoleModel", related_name="users", through="tb_system_user_role", forward_key="role_id", backward_key="user_id", description="用户角色表")

    class Meta:
        table = "tb_system_user"
        table_description = "系统用户表"
        manager = DefaultFilterManager()